<template>
  <div>
    <!-- 添加提示弹出层 -->
    <el-dialog
      title="添加部门"
      :visible="addDialogVisible"
      @close="onClickClose"
    >
      <!-- 表单组件  el-form   label-width设置label的宽度   -->
      <!-- 匿名插槽 -->
      <el-form ref="companyForm" :model="formData" :rules="rules" label-width="120px">
        <el-form-item prop="name" label="部门名称">
          <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item prop="code" label="部门编码">
          <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item prop="manager" label="部门负责人">
          <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" />
        </el-form-item>
        <el-form-item prop="introduce" label="部门介绍">
          <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
      <el-row slot="footer" type="flex" justify="center">
        <!-- 列被分为24 -->
        <el-col :span="6">
          <el-button type="primary" size="small">确定</el-button>
          <el-button size="small" @click="$emit('update:add-dialog-visible',false)">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getDapartmentList } from '@/api/dapartment'
export default {
  filters: {},
  components: {},
  props: {
    addDialogVisible: {
      type: Boolean,
      required: true
    },
    treeNode: {
      type: Object,
      required: true
    }
  },
  data() {
    // 检查部门名称是否重复
    const isRepeatDeptsName = (rules, val, callback) => {
      // console.log(this.treeNode)
      if (this.treeNode.children) {
        const valid = this.treeNode.children.some(item => item.name === val)
        if (valid) {
          return callback(new Error('部门已存在'))
        }
      }
      callback()
    }
    // 检查部门编码是否重复
    const isRepeatCode = async(rules, val, callback) => {
      const res = await getDapartmentList()
      console.log(res)
      const valid = res.depts.some(item => item.code === val)
      if (valid) {
        return callback(new Error('部门已存在'))
      }
      callback()
    }
    return {
      // 定义表单数据
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      // 定义校验规则
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: isRepeatDeptsName, trigger: 'blur' }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: isRepeatCode, trigger: 'blur' }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onClickClose() {
      this.$refs.companyForm.resetFields()
      this.$emit('update:add-dialog-visible', false)
    }
  }
}
</script>

<style scoped lang='less'>

</style>
